<template>
  <view>
    <view class="search-box" @click.native="goToSearch"><search></search></view>
    <!-- 轮播图 -->
    <swiper
      :indicator-dots="true"
      :autoplay="true"
      :interval="3000"
      :duration="1000"
      circular="true"
    >
      <swiper-item v-for="(item, index) in list" :key="index">
        <navigator
          :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id"
          class="swiper-item"
        >
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航 -->
    <view class="nav-list">
      <view
        class="nav-item"
        v-for="(item, index) in navList"
        :key="index"
        @click="navHander(item)"
      >
        <image class="nav-item-img" :src="item.image_src"></image>
      </view>
    </view>

    <!-- 底部数据 -->
    <view class="bottom-list">
      <view
        class="bottom-item"
        v-for="(item, index) in bottomList"
        :key="index"
      >
        <!-- 标题 -->
        <image
          class="bottom-item-title"
          :src="item.floor_title.image_src"
        ></image>
        <!-- 图片 -->
        <view class="bottom-img-box">
          <navigator class="img-left-box" :url="item.product_list[0].url">
            <image
              class="left-box-image"
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
            ></image>
          </navigator>
          <view class="img-right-box">
            <navigator
              class="right-box-item"
              v-for="(image, i) in item.product_list"
              :key="i"
              v-if="i !== 0"
              :url="image.url"
            >
              <image
                class="right-box-item-img"
                :src="image.image_src"
                :style="{ width: image.image_width + 'rpx' }"
                mode="widthFix"
              ></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import mixins from '@/mixins/tabbar-icon.js'
export default {
  mixins: [mixins],
  data() {
    return {
      list: [], //轮播图数据
      navList: [], //分类
      bottomList: [] //底部数据
    }
  },
  onLoad() {
    //获取轮播图数据
    this.getImgList()
    //获取分类列表
    this.getNavList()
    //获取底部数据
    this.getBottomData()
  },
  methods: {
    async getImgList() {
      const { data } = await uni.$http.get('/api/public/v1/home/swiperdata')
      //请求失败
      if (data.meta.status !== 200) return uni.$errShowMes()
      this.list = data.message
    },
    async getNavList() {
      const { data } = await uni.$http.get('/api/public/v1/home/catitems')
      if (data.meta.status !== 200) return uni.$errShowMes()
      this.navList = data.message
    },
    //判断是否分类
    navHander(item) {
      if (item.name === '分类') {
        uni.switchTab({
          url: '/pages/cate/cate'
        })
      }
    },
    //获取底部图文
    async getBottomData() {
      const { data } = await uni.$http.get('/api/public/v1/home/floordata')
      if (data.meta.status !== 200) return uni.$errShowMes()
      // 通过双层 forEach 循环，处理 URL 地址
      data.message.forEach(floor => {
        floor.product_list.forEach(prod => {
          prod.url =
            '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
        })
      })
      this.bottomList = data.message
    },
    //跳转serach页面
    goToSearch() {
      uni.navigateTo({
        url: '/subpkg/search/search'
      })
    }
  }
}
</script>

<style lang="scss">
swiper {
  height: 320rpx;

  .swiper-item,
  image {
    height: 100%;
    width: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin-top: 10rpx;

  .nav-item-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.bottom-item {
  .bottom-item-title {
    height: 80rpx;
    width: 100%;
    display: flex;
  }
}

.img-right-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.bottom-item {
  margin-top: 7rpx;

  .bottom-img-box {
    display: flex;
    // padding-left: 5rpx;
  }
}
.search-box {
  position: sticky;
  top: 0;
  z-index: 999;
}
</style>
